<?php
require '../script/mysqlconfig.php';

$name = $_POST['name'];
$pass = $_POST['pass'];
$group = $_POST['group'];
$role = $_POST['role'];
$email = $_POST['email'];

/* 对密码加密，保证数据的安全*/
$pass_hash = password_hash($_POST["pass"], PASSWORD_DEFAULT);

/* 连接数据库*/
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_error) {
    die("数据库连接错误：" . $mysqli->connect_error);
}
/* 向数据表中传入用户的数据，方便后面对登录进行验证*/
if (isset($_POST["reg"])) {
    $sql = "INSERT INTO `users` (`name`, `password`, `group`, `role`, `info`) VALUES ('$name','$pass_hash','$group','$role','$email')";
    $mysqli->query($sql);
    if ($mysqli->affected_rows > 0) {
//        echo "<script>alert('账号创建成功!');history.go(-1);</script>";
        echo "账号创建成功!";
    } else {
        die("账号创建失败!" . $mysqli->connect_error);
    }
}
// 关闭数据库连接
mysqli_close($mysqli);
